﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Nasty.Characters;

namespace Nasty
{
    public class FileAcess
    {
        //private List<Character> list = new List<Character>();

        #region Profile Management
        public Profile loadProfile(string selection)
        {
            //no need to create a new object of mythingtoload
            Profile characterToLoad;
            //this is another way to open up FileStream, but only if you are
            //certain that it and the directory exists, if not check first.
            string target = Directory.GetCurrentDirectory() + "\\Saves\\" + selection + ".sav";
            FileStream file = new FileStream(target, FileMode.Open);
            //We load our Serializer the same way    
            XmlSerializer serialize = new XmlSerializer(typeof(Profile));
            //Serializer is creating our object of us. The (GeneralObject) is
            //A cast telling it to load the data like a GeneralObject.
            characterToLoad = (Profile)serialize.Deserialize(file);
            file.Close();
            return characterToLoad;
        }
        
        //La méthode de sauvegarde renvoie 0 si la sauvegarde est réussi, ou -1 si elle échoue.
        public int saveProfile(Profile profileToSave)
        {
            int result = 0;
            //First off an easy to find directory
            string target = Directory.GetCurrentDirectory() + "\\Saves\\";
            if (Directory.Exists(target) == false)
            {
                Directory.CreateDirectory(target);
            }
            target = target + profileToSave.PlayerName + ".sav";

            FileStream file = File.Open(target, FileMode.OpenOrCreate);
            //Next we need our serializer to get our data organized
            try
            {

                XmlSerializer serialize = new XmlSerializer(typeof(Profile));
                //Tell it to do the work and save it.
                serialize.Serialize(file, profileToSave);
            }
            catch (Exception xcp)
            {
                MessageBox.Show(xcp.ToString());
            }
            //ALWAYS remember to close your file when you're done with it.
            file.Close();
            return result;
        }
        #endregion

        #region CHR File management
        //Write the generate character
        public int writeCHRFile(Character characterToSave, string projectName, string targetDirectory)
        {
            int result = 0;
            //First off an easy to find directory
            string target = targetDirectory + "\\" + projectName;
            if (Directory.Exists(target) == false)
            {
                Directory.CreateDirectory(target);
            }
            target = target + projectName + ".chr";

            FileStream file = File.Open(target, FileMode.OpenOrCreate);
            //Next we need our serializer to get our data organized
            try
            {

                XmlSerializer serialize = new XmlSerializer(typeof(Character));
                //Tell it to do the work and save it.
                serialize.Serialize(file, characterToSave);
            }
            catch (Exception xcp)
            {
                MessageBox.Show(xcp.ToString());
            }
            //ALWAYS remember to close your file when you're done with it.
            file.Close();
            return result;
        }

        //Write a temporary character file to prevent unattended closing
        public int writeCHRTemp(Character characterToSave, string projectName, string targetDirectory)
        {
            int result = 0;
            //First off an easy to find directory
            string target = targetDirectory + "\\" + projectName;
            if (Directory.Exists(target) == false)
            {
                Directory.CreateDirectory(target);
            }
            target = target + projectName + ".chr.tmp";

            FileStream file = File.Open(target, FileMode.OpenOrCreate);
            //Next we need our serializer to get our data organized
            try
            {

                XmlSerializer serialize = new XmlSerializer(typeof(Character));
                //Tell it to do the work and save it.
                serialize.Serialize(file, characterToSave);
            }
            catch (Exception xcp)
            {
                MessageBox.Show(xcp.ToString());
            }
            //ALWAYS remember to close your file when you're done with it.
            file.Close();
            return result;
        }

        public Character readCHRFile(string charToLoad)
        {
            Character newChar = new Character();
            //this is another way to open up FileStream, but only if you are
            //certain that it and the directory exists, if not check first.
            string target = charToLoad;
            FileStream file = new FileStream(target, FileMode.Open);
            //We load our Serializer the same way    
            XmlSerializer serialize = new XmlSerializer(typeof(Character));
            //Serializer is creating our object of us. The (GeneralObject) is
            //A cast telling it to load the data like a GeneralObject.
            newChar = (Character)serialize.Deserialize(file);
            file.Close();
            return newChar;
        }

        #endregion

        //Permet de lister les sauvegardes disponibles pour les charger
        public List<String> viewExistingProfiles()
        {
            //First off an easy to find directory
            if (Directory.Exists("\\Saves") == false)
            {
                Directory.CreateDirectory("\\Saves");
            }
            string[] profiles = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\Saves");
            
            List<String> profilesList = new List<string>();
            foreach (string profile in profiles)
            {
                if (Path.GetExtension(profile) == ".sav")
                {
                    profilesList.Add(Path.GetFileNameWithoutExtension(profile));
                }
            }
            return profilesList;
        }


    }
}
